<?php

namespace app\admin\controller;

use classes\TokenBucket;
use controller\BasicAdmin;
use service\LogService;
use service\DataService;
use think\Db;
use think\exception\HttpResponseException;
use think\facade\Cache;

/**
 * 投票配置控制器
 */
class InfoConfig extends BasicAdmin
{

    /**
     * 当前默认数据模型
     * @var string
     */
    public $vote = 'tp_vote';
    public $lottery = 'tp_lottery';

    /**
     * 常规配置
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function setting()
    {
        if ($this->request->isGet()) {
            $data = Db::name($this->vote)->find();
            return $this->fetch('', ['vo' => $data, 'title' => '设置']);
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['regdate'] = strtotime($data['regdate']);
            $data['regenddate'] = strtotime($data['regenddate']);
//            $data['statdate'] = strtotime($data['statdate']);
//            $data['enddate'] = strtotime($data['enddate']);


            unset($data['id']);
            Db::name($this->vote)->where('id', 1)->update($data);
            $new_data = Db::name($this->vote)->find();

            Cache::store('redis')->rm('total_vcount');
            Cache::store('redis')->rm('view_count');

            Cache::store('redis')->inc('total_vcount',$new_data['total_vcount']);
            Cache::store('redis')->inc('view_count',$new_data['view_count']);

            Cache::store('redis')->handler()->hMset('vote:config', $new_data);
            $this->success('配置成功！', '');
        }
    }

    /**
     * 投票配置
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function vote_setting()
    {
        if ($this->request->isGet()) {
            $data = Db::name($this->vote)->find();
            $data['left_limit_num'] = intval(Cache::store('redis')->handler()->LLEN('mycontainer')); //令牌桶剩余数

            return $this->fetch('', ['vo' => $data, 'title' => '设置']);
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($data['warn_status']) {
                if ($data['warn_min'] < 1 || $data['warn_vote'] < 1) {
                    $this->error('警告分钟数、投票数不能小于1！', '');
                }
            }
            if ($data['lock_status']) {
                if ($data['lock_min'] < 1
                    || $data['lock_vote'] < 1
                    || $data['lock_hour'] < 1
                    || $data['lock_hour_vote'] < 1)
                {
                    $this->error('锁定分钟数、小时数、投票数不能小于1！', '');
                }
            }

            unset($data['id']);
            Db::name($this->vote)->where('id', 1)->update($data);
            $new_data = Db::name($this->vote)->find();
            Cache::store('redis')->handler()->hMset('vote:config', $new_data);

            //如果开启流量限制，重设令牌桶，填满令牌
            if ($data['total_limit_status']) {
                TokenBucket::reset();
            }
            $this->success('投票配置成功！', '');
        }
    }

    /**
     * 抽奖配置
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function lottery_setting()
    {
        if ($this->request->isGet()) {
            $data = Db::name($this->lottery)->find();
            $today = date("Y-m-d");
            $data['day_count1'] = Cache::store('redis')->handler()->LLEN('lottery:day:prize:1:time:' . $today);
            $data['day_count2'] = Cache::store('redis')->handler()->LLEN('lottery:day:prize:2:time:' . $today);
            $data['day_count3'] = Cache::store('redis')->handler()->LLEN('lottery:day:prize:3:time:' . $today);
            $data['day_count4'] = Cache::store('redis')->handler()->LLEN('lottery:day:prize:4:time:' . $today);

            return $this->fetch('', ['vo' => $data, 'title' => '设置']);
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();
//            $data['statdate'] = strtotime($data['statdate']);
//            $data['enddate'] = strtotime($data['enddate']);
            unset($data['id']);
            Db::name($this->lottery)->where('id', 1)->update($data);
            $new_data = Db::name($this->lottery)->find();
            Cache::store('redis')->handler()->hMset('lottery:config', $new_data);


            Cache::store('redis')->handler()->del('lottery:prize:1');
            Cache::store('redis')->handler()->del('lottery:prize:2');
            Cache::store('redis')->handler()->del('lottery:prize:3');
            Cache::store('redis')->handler()->del('lottery:prize:4');

            $lottery = Db::name('tp_lottery')->find();

            //1,2,3,4等奖剩余数量
            $lottery1count = Db::name('tp_lottery_record')
                ->where('prize', 1)
                ->count();

            if ($lottery['firstnums'] - $lottery1count > 0) {
                for ($i = 1; $i <= ($lottery['firstnums'] - $lottery1count); $i++) {
                    Cache::store('redis')->handler()->LPUSH('lottery:prize:1', 1); //队列
                }
            }
            $lottery2count = Db::name('tp_lottery_record')
                ->where('prize', 2)
                ->count();

            if ($lottery['secondnums'] - $lottery2count > 0) {
                for ($i = 1; $i <= ($lottery['secondnums'] - $lottery2count); $i++) {
                    Cache::store('redis')->handler()->LPUSH('lottery:prize:2', 1); //队列
                }
            }
            $lottery3count = Db::name('tp_lottery_record')
                ->where('prize', 3)
                ->count();

            if ($lottery['thirdnums'] - $lottery3count > 0) {
                for ($i = 1; $i <= ($lottery['thirdnums'] - $lottery3count); $i++) {
                    Cache::store('redis')->handler()->LPUSH('lottery:prize:3', 1); //队列
                }
            }
//            $lottery4count = Db::name('tp_lottery_record')
//                ->where('prize', 4)
//                ->count();
//
//            if ($lottery['fournums'] - $lottery4count > 0) {
//                for ($i = 1; $i <= ($lottery['fournums'] - $lottery4count); $i++) {
//                    Cache::store('redis')->handler()->LPUSH('lottery:prize:4', 1); //队列
//                }
//            }

            //1,2,3,4等奖今日已发放的数量
            $today = date("Y-m-d");
            $tomorrow = strtotime(date("Y-m-d", strtotime("+1 day")));

            Cache::store('redis')->handler()->del('lottery:day:prize:1:time:' . $today);
            Cache::store('redis')->handler()->del('lottery:day:prize:2:time:' . $today);
            Cache::store('redis')->handler()->del('lottery:day:prize:3:time:' . $today);
            Cache::store('redis')->handler()->del('lottery:day:prize:4:time:' . $today);

            $lotteryToday = Db::name('tp_lottery_record')
                ->where('prize > 0')
                ->whereTime('time', 'today')
                ->select();

            if (!empty($lotteryToday)) {
                foreach ($lotteryToday as $one) {
                    Cache::store('redis')->handler()->LPUSH('lottery:day:prize:' . $one['prize'] . ':time:' . $today, 1); //队列
                    Cache::store('redis')->handler()->expireat('lottery:day:prize:' . $one['prize'] . ':time:' . $today, $tomorrow);//设置过期时间
                }
            }


            Cache::store('redis')->handler()->del('redpacket:left');
            //红包数量
            $redpacketList = Db::name('tp_redpacket')->where('status', 0)->select();
            if (!empty($redpacketList)) {
                foreach ($redpacketList as $one) {
                    Cache::store('redis')->handler()->LPUSH('redpacket:left', $one['id']); //队列
                }
            }


            $this->success('抽奖配置成功！', '');
        }
    }

}
